//
//  TPNavigationBarView.m
//  Pods
//
//  Created by caixun on 15/11/6.
//
//

#import "TPNavigationBarView.h"
#import "TPImageView.h"


#define TP_NavBar_Start_X (0)   /// NavBar的起始X值
#define TP_NavBar_Start_Y (0)   /// NavBar的起始Y值

#define TP_NavBar_Btn_H (44)       /// 左右Button的高度
#define TP_NavBar_Btn_W (80)       /// 左右各有一个Button的宽度

#define TP_NavBar_ViewTitle_Font [UIFont systemFontOfSize:18.0f] /// idNavTitle的Font
#define TP_NavBar_Btn_Font       [UIFont systemFontOfSize:15.0f] /// Button的Font

#define TP_NavBar_ViewTitle_Tag   (1412181046)   /// idNavTitle的Tag
#define TP_NavBar_BtnLeft_Tag     (1506052122)   /// 左Button的Tag
#define TP_NavBar_BtnRight_Tag    (1506052123)   /// 右Button的Tag，右侧只有一个button时使用

#define TP_NavBar_BgColor           [UIColor redColor]     /// NavBar背景的颜色
#define TP_NavBar_TitleColor        [UIColor blackColor]     /// NavBar中间Title的颜色
#define TP_NavBar_BottomLine_Color  [UIColor blackColor]     /// NavBar底部描边的颜色

// 判断obj是否为className类型
#define TP_Object_Is_Class(obj,className) [obj isKindOfClass:[className class]]

/// 是否NSArray类型（单独处理NSMutableArray）
#define TP_Ary_Class(ary) TP_Object_Is_Class(ary, NSArray)
#define TP_mAry_Class(mary) TP_Object_Is_Class(mary,NSMutableArray)

/// 是否有效，不为空，且是NSArray类型，且count值大于0（单独处理NSMutableArray）
#define TP_Ary_Is_Valid(ary) ((ary) && (TP_Ary_Class(ary)) && ([ary count] > 0))
#define TP_mAry_Is_Valid(mary) ((mary) && (TP_mAry_Class(mary)) && ([mary count] > 0))

/// 是否无效，或为空，或不是NSArray类型，或count值小于等于0（单独处理NSMutableArray）
#define TP_Ary_Not_Valid(ary) ((!ary) || (!TP_Ary_Class(ary)) || ([ary count] <= 0))
#define TP_mAry_Not_Valid(mary) ((!mary) || !(TP_mAry_Class(mary)) || ([mary count] <= 0))


@interface TPNavigationBarView ()

@end

@implementation TPNavigationBarView

#pragma mark - *********************************** View Lifecycle **********************************

- (id)initWithTitle:(NSString *)strTitle
{
    self = [super init];
    
    if (self)
    {
        /// 设置frame和背景色
        self.frame = CGRectMake(TP_NavBar_Start_X, TP_NavBar_Start_Y, TP_NavBar_W, TP_NavBar_H);
        self.backgroundColor = TP_NavBar_BgColor;
        
        /// 背景图片视图，换肤用
        _imgvBackGround = [TPImageView getWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
                                               image:[UIImage imageNamed:@""]];
        _imgvBackGround.image = [UIImage imageNamed:@""];
        [self addSubview:_imgvBackGround];
        
        /// 标题
        _labelTitle = [[UILabel alloc] init];
        _labelTitle.frame = CGRectMake(0, 0, screenW-80*2, 30);
        _labelTitle.center = CGPointMake(screenW/2, TP_NavBar_H/2+10);
        _labelTitle.backgroundColor = [UIColor clearColor];
        _labelTitle.font = TP_NavBar_ViewTitle_Font;
        _labelTitle.text = strTitle;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.textColor = TP_NavBar_TitleColor;
        [self addSubview:_labelTitle];
        
        /// 底部描边
        /// 通过scale属性判断当前设备的像素密度，如果小于1时高度等于1屏幕上才能看到，大于1时显示0.5屏幕上也可见
        CGFloat fltBottomLineH = [self scaleAdaptWidth:1.0];
        _viewBottomLine = [[UIView alloc] init];
        _viewBottomLine.frame = CGRectMake(0, TP_NavBar_H-fltBottomLineH, screenW, fltBottomLineH);
        _viewBottomLine.backgroundColor = TP_NavBar_BottomLine_Color;
        _viewBottomLine.hidden = NO;
        [self addSubview:_viewBottomLine];
    }
    return self;
}

- (void)dealloc
{
    
}


#pragma mark - *********************************** Init All Views **********************************

/// 创建左侧按钮
- (void)createNavLeftBtnWithItem:(id)idItem
{
    if (!_btnLeft)
    {
        _btnLeft = [TPButton getWithFrame:CGRectMake(15, 20, TP_NavBar_Btn_W, TP_NavBar_Btn_H)];
        _btnLeft.tag = TP_NavBar_BtnLeft_Tag;
        _btnLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _btnLeft.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        _btnLeft.titleLabel.adjustsFontSizeToFitWidth = YES;
        
        /// 高亮时的文字颜色
        [_btnLeft setTitleColor:TP_NavBar_BottomLine_Color forState:UIControlStateHighlighted];
        /// 不可点击时的文字颜色
        [_btnLeft setTitleColor:TP_NavBar_BottomLine_Color forState:UIControlStateDisabled];
        
        [self addSubview:_btnLeft];
    }
    
    if (TP_Object_Is_Class(idItem, NSString)) /// NSString
    {
        if (![idItem isEqualToString:@""]) /// 有标题
        {
            // 这块设置颜色是没用的，这个库里面就没有nav_btn_back图片，要想改图片去自己的项目里面改去
            [_btnLeft setNorBgImg:[UIImage imageNamed:@"nav_btn_back"]
                         norTitle:idItem
                    norTitleColor:TP_NavBar_TitleColor
                        titleFont:TP_NavBar_Btn_Font];
        }
        else /// 没有标题，直接设置返回按钮图片
        {
            // 这块设置颜色是没用的，这个库里面就没有nav_btn_back图片，要想改图片去自己的项目里面改去
            [_btnLeft setImage:[UIImage imageNamed:@"nav_btn_back"] forState:UIControlStateNormal];
        }
    }
    else if (TP_Object_Is_Class(idItem, UIImage)) /// UIImage类型
    {
        [_btnLeft setImage:idItem forState:UIControlStateNormal];
    }
}

/// 创建右侧按钮
- (void)createNavRightBtnWithItem:(id)idItem
{
    if (!_btnRight)
    {
        _btnRight = [TPButton getWithFrame:CGRectMake(screenW - TP_NavBar_Btn_W, 20, TP_NavBar_Btn_W, TP_NavBar_Btn_H)];
        _btnRight.tag = TP_NavBar_BtnRight_Tag;
        _btnRight.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _btnRight.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 12);
        _btnRight.titleLabel.adjustsFontSizeToFitWidth = YES;
        
        /// 普通状态时的文字颜色
        [_btnRight setTitleColor:TP_NavBar_TitleColor forState:UIControlStateNormal];
        /// 高亮时的文字颜色
        [_btnLeft setTitleColor:TP_NavBar_BottomLine_Color forState:UIControlStateHighlighted];
        /// 不可点击时的文字颜色
        [_btnLeft setTitleColor:TP_NavBar_BottomLine_Color forState:UIControlStateDisabled];
        _btnRight.titleLabel.font = TP_NavBar_Btn_Font;
        
        [self addSubview:_btnRight];
    }
    
    if (TP_Object_Is_Class(idItem, NSString)) /// String类型
    {
        /// 设置title
        [_btnRight setTitle:idItem forState:UIControlStateNormal];
    }
    else if (TP_Object_Is_Class(idItem, UIImage)) /// UIImage类型
    {
        /// 设置image
        [_btnRight setImage:idItem forState:UIControlStateNormal];
    }
}

/// 设置中间标题
- (void)setIdNavTitle:(id)idNavTitle
{
    /// 判断idNavTitle的类型，目前支持String、View
    
    if ([idNavTitle isKindOfClass:[NSString class]]) /// String类型
    {
        _labelTitle.text = idNavTitle;
    }
    else if ([idNavTitle isKindOfClass:[UIView class]]) /// View类型
    {
        /// 删除原来的View
        UIView *viewOldNavTitle = [self viewWithTag:TP_NavBar_ViewTitle_Tag];
        [viewOldNavTitle removeFromSuperview];
        viewOldNavTitle = nil;
        
        /// 添加新的View
        UIView *viewNewNavTitle = idNavTitle;
        viewNewNavTitle.tag = TP_NavBar_ViewTitle_Tag;
        
        if (viewNewNavTitle.frame.size.width > (screenW - TP_NavBar_Btn_W*2)) /// 如果中间视图超出最大宽度，重新设定
        {
            viewNewNavTitle.frame = CGRectMake(viewNewNavTitle.frame.origin.x,
                                               viewNewNavTitle.frame.origin.y,
                                               screenW - TP_NavBar_Btn_W*2,
                                               viewNewNavTitle.frame.size.height);
        }
        
        /// 视图居中
        viewNewNavTitle.center = CGPointMake(screenW/2, (TP_NavBar_H-20)/2+20);
        [self addSubview:viewNewNavTitle];
    }
}

- (CGFloat)scaleAdaptWidth:(CGFloat)fltWidth
{
    /// 如果宽度为0，则直接返回
    if (fltWidth == 0)
    {
        return fltWidth;
    }
    
    /// 获取屏幕的像素密度，如iPhone6Plus，一个点上有3个像素，该值为3
    CGFloat fltScale = [UIScreen mainScreen].scale;
    /// 传入的参数最小值要求为1，如果小于1，则设为1，保证像素最小值为1
    if (fltWidth < 1.0)
    {
        fltWidth = 1.0;
    }
    /// 要显示的点除以屏幕密度，为要返回的点，比实际传进来的要小
    return (fltScale > 0 ? (fltWidth / fltScale) : fltWidth);
}


@end
